home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12032 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  104 lines

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader Jr)
  3. Newsgroups: gnu.g++.help,comp.lang.c++,panix.questions
  4. Subject: Why not?  c++ Array of strings...
  5. Followup-To: gnu.g++.help,comp.lang.c++
  6. Date: 17 Mar 1996 17:30:17 -0500
  7. Organization: Panix
  8. Message-ID: <4ii3pp$4df@panix.com>
  9. NNTP-Posting-Host: panix.com
  10.  
  11. Note Followup
  12.  
  13. I want a class to a have a member that is an array of
  14. character strings that are dynamically allocated.  I have written 
  15. a very simple program that shows the eveolution of my thought
  16. that leads me to beleieve that I should be able to do what I
  17. am trying, but I get a segmentation fault and core dump when I
  18. try it.  Why?  I obviously am missing something.  I have kept
  19. the code as brief as possible (I'm using g++ v2.4):
  20.  
  21.  
  22. ***foo.h
  23. class Foo {
  24.     public:
  25.         Foo();      // constructor
  26.         ~Foo();     // destructor
  27.     private:
  28.         char a[10];  // declare and define an array of characters
  29.         char *b;     // declare a string pointer
  30.         char *c[10]; // declare an array of ten strings?
  31. };
  32.  
  33. ***foo.C
  34. nclude <iostream.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include "foo.h"
  38.  
  39. // Constructor -- all the action is here --
  40. Foo::Foo()
  41. {
  42.         // populate the character array
  43.         a[0] = 'a';
  44.         a[1] = 'r';
  45.         a[2] = 't';
  46.         a[3] = 'h';
  47.         a[4] = 'u';
  48.         a[5] = 'r';
  49.  
  50.         // print the character array
  51.         cout << "output from array of char: ";
  52.         for(int i = 0 ; i < 6 ; i++)
  53.                 cout << a[i];
  54.  
  55.         cout << endl;
  56.  
  57.         // populate the string
  58.         b = new char[7];                // allocate space
  59.         assert( b != 0);                // make sure space was allocated
  60.         strcpy(b, "arthur");
  61.  
  62.         // print the string
  63.         cout << "output from *char: " << b;
  64.  
  65.         cout << endl;
  66.  
  67.         // So far so good.  This is where it all falls apart
  68.         // populate array of strings
  69.         c[0] = new char[10];    // allocate space
  70.         assert(c[0] != 0);      // make sure space was allocated
  71.         strcpy(c[0], "arthur");
  72.         c[1] = new char[10];
  73.         assert(c[1] != 0);
  74.         strcpy(c[1], "cinader");
  75.         c[2] = new char[10];
  76.         assert(c[2] != 0);
  77.         strcpy(c[2], "jr.");
  78.  
  79.         // output array of strings
  80.         for (i = 0 ; i < 3 ; i--)
  81.                 cout << c[i] << " ";
  82.  
  83.         cout << endl;
  84.  
  85. }
  86.  
  87. Foo::~Foo() {
  88.         // clean up the mess
  89.         delete [] b;
  90.         for(int i = 0; i < 3; i --)
  91.                 delete [] c[i];
  92. }
  93.  
  94.  
  95. **** foo.driver.C
  96. #include "foo.h"
  97.  
  98. main()
  99. {
  100.         Foo f;
  101.  
  102.         return 0;
  103. }
  104.